package org.nutz.easy.model;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.nutz.dao.Dao;
import org.nutz.dao.impl.NutDao;
import org.nutz.json.Json;
import org.nutz.json.JsonException;
import org.nutz.lang.Files;
import org.nutz.lang.Strings;


/**
 * <p>数据库连接源</p>
 * @author defier Lai
 * <p>Date: 2009-10-1 下午02:22:49</p>
 * @version 1.0
 */
public class EasyDataSource {

	private static HashMap<String, Dao> daoMap = new HashMap<String, Dao>();
	
	private final static String DS_TYPE_MYSQL = "mysql";
	private final static String DS_TYPE_ORACLE = "oracle";
	
	private final static String DS_DEFAULT = "default";
	
	static {
		try {
			initDataSource();
		} catch (JsonException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public EasyDataSource() {
		
	}
	
	public static Dao get(String database) {
		if(Strings.isEmpty(database)) {
			database = DS_DEFAULT;
		}
		Dao dao = daoMap.get(database);
		if(dao == null) {
			throw new IllegalArgumentException("database " + database + " is not exist!");
		}
		return dao;
	}
	
	public static Dao get() {
		return daoMap.get(DS_DEFAULT);
	}
	
	@SuppressWarnings("unchecked")
	private static void initDataSource() throws JsonException, FileNotFoundException {
		File f = Files.findFile("database.json");
		Map<String, Object> map = (Map<String, Object>) Json.fromJson(new InputStreamReader(new FileInputStream(f)));
		for(String key : map.keySet()) {
			Map m = (Map)map.get(key);
			if(m.get("adapter") == null) {
				throw new IllegalArgumentException("adapter is not setting!");
			}
			DataSource dataSource = null;
			if(String.valueOf(m.get("adapter")).equalsIgnoreCase(DS_TYPE_MYSQL)) {
				dataSource = setMysqlDataSource(m);
			} else if(String.valueOf(m.get("adapter")).equalsIgnoreCase(DS_TYPE_ORACLE)) {
				// TODO 支持oracle
			}
			daoMap.put(key, new NutDao(dataSource));
		}
	}
	
	private static DataSource setMysqlDataSource(Map<String, Object> map) {
		if(map.get("database") == null) {
			throw new IllegalArgumentException("database is not setting!");
		}
		if(map.get("username") == null) {
			throw new IllegalArgumentException("username is not setting!");
		}
		
		String host = (map.get("host") != null) ? String.valueOf(map.get("host")) : "127.0.0.1";
		int port = (map.get("port") != null) ? ((Integer) map.get("port")) : 3306;
		String encoding = (map.get("encoding") != null) ? String.valueOf(map.get("encoding")) : "utf8";
		String driver = (map.get("driver") != null) ? String.valueOf(map.get("driver")) : "com.mysql.jdbc.Driver";
		
		BasicDataSource ds = new BasicDataSource();
		ds.setUsername(String.valueOf(map.get("username")));
		ds.setPassword(String.valueOf(map.get("password")));
		ds.setUrl("jdbc:mysql://" + host + ":" + port + "/" + String.valueOf(map.get("database")) + "?characterEncoding=" + encoding);
		ds.setDriverClassName(driver);
		return ds;
	}
	
	
}
